package cl.mondo.praha;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import cl.mondo.praha.global.ConnectionDetector;
import cl.mondo.praha.global.GooglePlaces;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void askCoordinates(View v) {
		GooglePlaces googlePlaces = new GooglePlaces();
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
		try {
			String latitude = ((TextView)findViewById(R.id.et_latitude)).getText().toString();
			String longitude = ((TextView)findViewById(R.id.et_longitude)).getText().toString();
			String radius = ((TextView)findViewById(R.id.et_radius)).getText().toString();
			googlePlaces.doQuestion(latitude, longitude, radius);
		} catch (Exception e) {
			alertDialog.setTitle(R.string.button_title_sorry);
			alertDialog.setMessage(e.getMessage());
			alertDialog.setNeutralButton(R.string.button_close, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			alertDialog.create().show();
		}
	}

	public void showNetworkConnection (View v) {
		ConnectionDetector connectionDetector = new ConnectionDetector(this);
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
		try {
			if (connectionDetector.isConnectingToInternet()) {
				NetworkInfo networkInfo = connectionDetector.getActiveNetworkInfo();
				CharSequence[] items = {
						getString(R.string.list_is_available) + ": " + networkInfo.isAvailable(),
						getString(R.string.list_is_connected) + ": " + networkInfo.isConnected(),
						getString(R.string.list_is_connected_or_connecting) + ": " + networkInfo.isConnectedOrConnecting(),
						getString(R.string.list_is_failover) + ": " + networkInfo.isFailover(),
						getString(R.string.list_is_roaming) + ": " + networkInfo.isRoaming(),
						getString(R.string.list_type) + ": " + networkInfo.getType(),
						getString(R.string.list_type_name) + ": " + networkInfo.getTypeName(),
						getString(R.string.list_subtype) + ": " + networkInfo.getSubtype(),
						getString(R.string.list_subtype_name) + ": " + networkInfo.getSubtypeName(),
						getString(R.string.list_state) + ": " + networkInfo.getState(),
						getString(R.string.list_detailed_state) + ": " + networkInfo.getDetailedState(),
						getString(R.string.list_reason) + ": " + networkInfo.getReason(),
						getString(R.string.list_extra_info) + ": " + networkInfo.getExtraInfo()};
				
				alertDialog.setTitle(R.string.button_title_information);
				alertDialog.setItems(items, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});

				alertDialog.setNeutralButton(R.string.button_close, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				
			} else {
				alertDialog.setTitle(R.string.button_title_sorry);
				alertDialog.setMessage(R.string.button_message_not_connected);
				alertDialog.setNeutralButton(R.string.button_close, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
			}
			alertDialog.create().show();
		} catch (Exception e) {
			alertDialog.setTitle(R.string.button_title_sorry);
			alertDialog.setMessage(e.getMessage());
			alertDialog.setNeutralButton(R.string.button_close, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			alertDialog.create().show();
		}
    }
}
